import cgi

from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import db

import urllib
from google.appengine.api import urlfetch

import sys,os
import httplib
import re
import string

import datastore
import library
import datetime 
from datetime import date


class MainPage(webapp.RequestHandler):

 def Write(self, div, fname,lname):
   self.response.out.write("<tr id=waiver_tr ><td>"+ div +"</td><td>"+ fname + "</td><td>" + lname + "</td><td></td><td></td></tr>" )



 def get(self):
    def compare( a,b):
         return cmp ( a[2], b[2] )


    start =  self.request.get('start') 
    end =  self.request.get('end') 
    page =  self.request.get('page') 

    tid="508303511"
    query="select __key__ from TournyPlayer  where tournament='"+ tid + "' order by lname"
    keys =  db.GqlQuery( query)


    name=""
    index=0
    players=[]

    players.append( ["NW4.0d","Analiza","Dolor"])
    players.append( ["NW4.0d","Diana","Nguyen"])
    players.append( ["NM4.5s","Matt","Reveno"])
    players.append( ["NW4.0s","Dianne","Alexander"])
    players.append( ["NX4.0d","Joel","Brown"])
    players.append( ["NM4.0s","Howard","Chen"])
    players.append( ["NX4.0d","Gia","Nguyen"])
    players.append( ["NM5.0s","Chris ","Medrano"])
    players.append( ["NM4.5s","Shiraz","Madan"])

    players.append( ["NX4.0d","Roger","Okamoto"])
    players.append( ["NX4.0d","Suzanne","Tiendas"])
    players.append( ["NX4.0d","Corey","Ebadat"])


    for k in keys:
      p = db.get(k)
      if( p.fname=="chanelle" and p.lname=="murphy"):
           p.fname = "Chanelle"
           p.lname = "Murphy"

      if( p.fname=="MICHAEL" and p.lname=="PERRANDO"):
           p.fname = "Michael"
           p.lname = "Perrando"

      if( p.fname=="robbie" and p.lname=="phillips"):
           p.fname = "Robbie"
           p.lname = "Phillips"


      players.append( [ p.event,p.fname,p.lname] ) 


    s = 0
    e = len(players) 

    if(start != ""): s = int(start)
    if(end != ""):   e = int(end)
    players = sorted( players , compare)

    if(page == "i"):  
       p = int(page)

       for i in range( s,e-1,p):
            f = open ("waiver.html")
            for line in f:
              self.response.out.write(line)                 
            f.close()
            for j in range(i,i+p):
               if( j < e):
                self.Write( players[j][0],players[j][1],players[j][2] )

       return





#   self.response.out.write(e)
    f = open ("waiver.html")
    for line in f:
      self.response.out.write(line)      


    for i in range( s, e ):
      self.Write( players[i][0],players[i][1],players[i][2] )

    self.response.out.write('<table >')

#<table align="center"  width="700" > 
#<thead><tr><th scope="col" align="left">First Name</th><th scope="col" align="left">Last Name</th><th align="left" scope="col">Signature</th>
#<p>
#<tr><td>Jennifer</td><td>Adams</td><td>jenyferadams@sbcglobal.net</td></tr> 
#</table>



application = webapp.WSGIApplication(
                                     [('/waiver', MainPage)],
                                     debug=True)

def main():
    run_wsgi_app(application)

if __name__ == "__main__":

#   print __file__
#   print __name__

    main()
